package com.hqyj.am.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.am.dao.DepartmentMapper;
import com.hqyj.am.pojo.Department;
import com.hqyj.am.pojo.ResultVo;
import com.hqyj.am.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
<<<<<<< HEAD
 * @since 2022-08-22
=======
 * @since 2022-08-18
>>>>>>> origin/master
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    @Autowired
    DepartmentMapper departmentMapper;

    //查询成功
    @Override
    public ResultVo queryByUserId(Integer userId) {
        ResultVo rv = new ResultVo();
        Department department = departmentMapper.queryByUserId(userId);
        if (department !=null){
            rv.setInfo("查询员工的部门成功");
            rv.setSuccess(true);
            return rv;
        }else{
            rv.setInfo("查询员工的部门失败");
            return rv;
        }
    }

    @Override
    public ResultVo insertDepartment(Department department) {
        ResultVo rv = new ResultVo();
        //创建条件构造器对象
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        //根据自增id查询
        boolean isDepartment = null != department.getDepartmentId() &&!department.getDepartmentId().equals("");
        queryWrapper.eq(isDepartment,"department_id",department.getDepartmentId());
        int insert = departmentMapper.insert(department);
        if (insert>0){
            rv.setInfo("添加成功了");
            rv.setSuccess(true);
        }else{
            rv.setInfo("添加失败了");
        }
        return rv;
    }

    @Override
    public ResultVo updateDepartment(Department department) {
        ResultVo rv = new ResultVo();
        //创建条件构造器对象
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        //根据自增id查询
        boolean isDepartment = null != department.getDepartmentId() &&!department.getDepartmentId().equals("");
        queryWrapper.eq(isDepartment,"department_id",department.getDepartmentId());
        int insert = departmentMapper.updateById(department);
        if (insert>0){
            rv.setInfo("修改成功了");
            rv.setSuccess(true);
        }else{
            rv.setInfo("修改失败了");
        }
        return rv;
    }

    @Override
    public ResultVo deleteDepartment(Department department) {
        ResultVo rv = new ResultVo();
        //创建条件构造器对象
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        //根据自增id查询
        boolean isDepartment = null != department.getDepartmentId() &&!department.getDepartmentId().equals("");
        queryWrapper.eq(isDepartment,"department_id",department.getDepartmentId());
        int insert = departmentMapper.deleteById(department);
        if (insert>0){
            rv.setInfo("删除成功了");
            rv.setSuccess(true);
        }else{
            rv.setInfo("删除失败了");
        }
        return rv;
    }
}
